package com.ztjava.core.base;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.client.Client;

public abstract class BaseValidator extends Validator{
	public BaseValidator(){
		super();
		setShortCircuit(true);
	}

	private boolean returnJson = false;
	private int errcode = 1;
	protected void setErrorCode(int code){
		this.errcode = 2;
	}
	protected void setReturnJson(boolean is){
		this.returnJson = is;
	}
	@Override
	protected void handleError(Controller c) {
		//通过客户端判断是否需要强制返回json数据
		if(returnJson==false&&c.getClass().isAnnotationPresent(ControllerClass.class)){
			try {
				Client client = (Client)c.getClass().getAnnotation(ControllerClass.class).client().newInstance();
				if(client.getReturnType()!=null&&client.getReturnType().equals("json")){
					setReturnJson(true);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
				
		String requestType = c.getRequest().getHeader(Constant.XRequestedWith);
		if((requestType!=null&&requestType.equals(Constant.XMLHttpRequest))||returnJson){
			c.setAttr(Constant.RESULT, Constant.ERROR);
			c.setAttr("errcode", errcode);
			c.renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"errcode"});
		}else {
			c.renderHtml("<p style='text-align:center;font-size:30px;line-height:50px'>"+c.getAttrForStr(Constant.MESSAGE)+"</p>");
		}
	}
	
	//校验电话号码正则表达式
	private static final String phonePattern="\\b(0(\\d{2,3})-\\d{6,9})\\b";
	//校验手机号码正则表达式
	private static final String mobilePattern="\\b(1[3,4,5,6,7,8,9]\\d{9})\\b";
	//校验电话手机号码正则表达式
	private static final String phoneMobilePattern="\\b((1[3,4,5,6,7,8,9]\\d{9})|(0(\\d{2,3})-\\d{6,9}))\\b";
  
  /**
   * 校验电话号码
   * @param field 校验字段
   * @param errorKey
   * @param errorMsg
   */
  protected void validatePhonePattern(String field,String errorKey,String errorMsg){
    validateRegex(field, phonePattern, false, errorKey, errorMsg);
  }
  protected void validateCardId(String field,String errorKey,String errorMsg){
	  String ls_id =  controller.getPara(field);
      if(ls_id.length() != 18){
    	  addError(errorKey, "身份证号长度不正确！");
    	  return;
      }
      char[] l_id = ls_id.toCharArray();
      int l_jyw = 0;
      int[] wi = new int[]{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1};
      char[] ai= new char[]{'1','0','X','9','8','7','6','5','4','3','2'};
      for(int i =0 ; i < 17; i++) {
          if(l_id[i] < '0' || l_id[i] > '9'){
        	  addError(errorKey, errorMsg);
        	  return;
          }
          l_jyw += (l_id[i] -'0')*wi[i];
      }
      l_jyw = l_jyw % 11;
      if(ai[l_jyw] != l_id[17]){
    	  addError(errorKey, errorMsg);
    	  return;
      }
  }
  
  /**
   * 验证手机号码
   * @param field
   * @param errorKey
   * @param errorMsg
   */
  protected void validateMobilePattern(String field,String errorKey,String errorMsg){
    validateRegex(field, mobilePattern, false, errorKey, errorMsg);
  }
  
  /**
   * 验证电话手机号码
   * @param field
   * @param errorKey
   * @param errorMsg
   */
  protected void validatePhoneMobilePattern(String field,String errorKey,String errorMsg){
    validateRegex(field, phoneMobilePattern, false, errorKey, errorMsg);
  }
}
